package com.tjetc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {
    @Bean
    //注入ServerEndpointExporter Bean对象,自动注册使用@ServerEndpoint该注解的Bean
    public ServerEndpointExporter serverEndpointExporter(){
        // 仅在非测试环境中创建ServerEndpointExporter bean
        // 避免在测试环境中出现"jakarta.websocket.server.ServerContainer not available"错误
        try {
            Class.forName("jakarta.websocket.server.ServerContainer");
            return new ServerEndpointExporter();
        } catch (ClassNotFoundException e) {
            // 在测试环境中，不创建ServerEndpointExporter
            return null;
        }
    }
}
